import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { machineScoreTypeArrs } from '../status.pipe';
import { CustomerMachineTestScoreService } from './customer-machine-test-score.service';

@Component({
  selector: 'app-customer-machine-test-score',
  templateUrl: './customer-machine-test-score.component.html',
  styleUrls: ['./customer-machine-test-score.component.scss']
})
export class CustomerMachineTestScoreComponent implements OnInit {

  searchOptions = [
    { type: 1, labelText: '综合搜索', valueKey: 'user', value: '', placeholder: 'ID/用户ID/手机号码/交易流水' },
    {
      type: 2,
      labelText: '收支类型',
      valueKey: 'type',
      value: [],
      multiple: true,
      selectBindKey: { key: 'value', label: 'label' },
      selectOptions: [...machineScoreTypeArrs],
    },
    { type: 3, labelText: '发生时间', valueKey: ['beginTime', 'endTime'], value: [], isTimeSelect: true }
  ];
  searchParams: any = {};

  tableParams: any = {
    loading: false,
    tableData: [],
    page: 1,
    pageSize: 20,
    total: 0
  };

  constructor(
    private message: NzMessageService,
    private services: CustomerMachineTestScoreService
  ) { }

  ngOnInit(): void {
  }

  async loadTable() {
    let params = {
      ...this.searchParams,
      page: this.tableParams.page,
      pageSize: this.tableParams.pageSize
    }
    this.tableParams.loading = true;
    const {code, data, message}: any = await this.services.getMachineScoreList(params);
    this.tableParams.loading = false;

    if (code === 0) {
      const {records, total} = data || {};
      this.tableParams.tableData = records || [];
      this.tableParams.total = total || 0;
    } else {
      this.message.error(message);
    }
  }

  queryResetData(params: any) {
    this.searchParams = params;
    this.tableParams.page = 1;
    this.loadTable();
  }

  // PageIndex Change
  onPageIndexChange(index: number) {
    this.tableParams.page = index;
    this.loadTable();
  }

  // PageSize Change
  onPageSizeChange(sizeNum: number) {
    this.tableParams.page = 1;
    this.tableParams.pageSize = sizeNum;
    this.loadTable();
  }
}
